/*=======================
 * 名称:newScroller
 * 功能：滚动条自定义
 ========================*/
function newScroller1(config, onend) {
	var swidth = $("#" + config.scrollerContain).width() / $("#" + config.scrollerMoveBox).width() * $("#" + config.scrollerArea).width(), //滚动条长度
	t = ($("#" + config.scrollerMoveBox).width() - $("#" + config.scrollerArea).width()) / ($("#" + config.scrollerArea).width() - swidth), //滚动条单位距离对应移动块单位
	st, //标记鼠标滚轮方向
	n = 0, //标记滚轮滚动单位
	speed = config.scrollerSpeed ? config.scrollerSpeed : 10, //滚轮速率
	smove = 0, //滚轮滚动移动距离
	move = 0, //鼠标拖拽移动距离
	left = 0, //标记滚动条左偏移
	leftd = 0, //去除left叠加
	isend=0,//标记到底
	startX, //标记起始位置
	dl = $('#'+config.scrollerArea).offset().left,//偏移量
	resizeTimer = null, //兼容chrome Ie8滚轮事件执行2次
	usescroller = false, //用户触发滚轮
	usemouse = false, //用户触发鼠标
	flg = false, //标记是否滑动
	flag = false, //标记长时间点击快速定位
	minmove = 0, //标记滚动条最小的移动范围
	maxmove = $("#" + config.scrollerArea).width() - swidth;
	//标记滚动条最大的移动范围
	$("#" + config.scrollerArea).children("div").width(swidth + 'px');
	init();
	/*鼠标操作滚动条 */
	$("#" + config.scrollerArea).live({
		mousedown : function(e) {
			e.preventDefault();
			//Firefox
			$("#" + config.scrollerArea).children("div").css("background-color", "#5555ff");
			usemouse = true;
			flg = true;
			flag = true;
			startX = e.clientX;
			// 保存鼠标刚按下时鼠标距离div起始点的距离
			usescroller ? left = leftd + move + n : left = leftd + move;
			n = 0;
			move = 0;
			usescroller = false;
			dl = $('#'+config.scrollerArea).offset().left;
			/*通过点击滚动条外部的快速定位滚动条 */
			if (e.pageX > (dl + swidth + left)) {
				left += swidth;
				setTimeout(function() {
					mousedownMoveR(e)
				}, 1000);
				if (left > maxmove) {
					left = $("#" + config.scrollerArea).width() - swidth;
					if(!isend){
						isend = 1;
						onend(config.scrollerMoveBox);
					}
				}
				leftd = left;
				mousedownMovin();
			} else if (e.pageX < (dl + left)) {
				left -= swidth;
				setTimeout(function() {
					mousedownMoveR(e)
				}, 1000);
				if (left < minmove) {
					left = 0;
				}
				leftd = left;
				mousedownMovin();
			}
		},
		mouseup : function() {
			flg = false;
			flag = false;
			$("#" + config.scrollerArea).children("div").css("background-color", "#9999ff");
		},
		mousemove : function(e) {
			flag = false;
			mouseMoveAction(e);
		}
	});
	/*鼠标点击快速定位   */
	function mousedownMovin() {
		$("#" + config.scrollerMoveBox).css("margin-left", -left * t);
		$("#" + config.scrollerArea).empty();
		$("#" + config.scrollerArea).append("<div class='scroller' style='margin-left:" + left + "px; height:100%; width:" + swidth + "px; background-color:#5555ff;'></div>");
	}
	/*鼠标长时间点击激活   */
	function mousedownMoveR(e) {
		e.preventDefault();
		//Opera/Chrome
		e.returnValue = false;
		//IE
		if (flg && flag) {
			left = e.pageX - swidth-dl;
			//  用户move的距离
			if (left < minmove) {
				left = 0;
			} else if (left > maxmove) {
				left = $("#" + config.scrollerArea).width() - swidth;
				onend(config.scrollerMoveBox);		
			}
			leftd = left;
			$("#" + config.scrollerMoveBox).animate({
				marginLeft : -left * t
			}, 500);
			$("#" + config.scrollerArea).children("div").animate({
				marginLeft : left
			}, 500);
		}

	}

	/*防止用户在导航区域外 松开鼠标   */
	$("html , .scrollerContain").mouseup(function() {
		flg = false;
		flag = false;
		$(".scroller").css("background-color", "#9999ff");
	})

	/*用户激活滚动条后在滚动区域外移动  */
	$("html").mousemove(function(e) {
		mouseMoveAction(e);
	})
	/*鼠标拖动操作  */
	function mouseMoveAction(e) {
		e.preventDefault();
		//Opera/Chrome
		e.returnValue = false;
		//IE
		if (flg) {
			move = e.clientX - startX;
			//  用户move的距离
			if ((left + move) < minmove) {
				left = 0;
				move = 0;
			} else if ((left + move) > maxmove) {
				left = $("#" + config.scrollerArea).width() - swidth;
				move = 0;	
				if(!isend){
					isend = 1;
					onend(config.scrollerMoveBox);
				}
			}
			leftd = left;
			$("#" + config.scrollerMoveBox).css("margin-left", -(left + move) * t);
			$("#" + config.scrollerArea).empty();
			$("#" + config.scrollerArea).append("<div class='scroller' style='margin-left:" + (left + move) + "px; height:100%; width:" + swidth + "px; background-color:#5555ff;'></div>");
			init();
		}

	}

	//滚轮事件绑定
	var scrollFunc = function(event) {
		var delta = 0;
		if (!event)/* For IE. */
			event = window.event;
		if (event.wheelDelta) {/* IE/Opera. */
			delta = event.wheelDelta / 120;
		} else if (event.detail) {
			delta = -event.detail / 3;
		}
		if (delta)
			resizeTimer = resizeTimer ? null : setTimeout(function() {
				scrollerMoving(delta);
			}, 0);

		if (event.preventDefault)
			event.preventDefault();
		event.returnValue = false;

	}
	/*滚轮操作滚动条   */
	$("#" + config.scrollerContain).live({
		mouseover : function() {

			/*注册事件*/
			if (document.addEventListener) {
				document.addEventListener('DOMMouseScroll', scrollFunc, false);
			}//W3C
			window.onmousewheel = document.body.onmousewheel = scrollFunc;
			//IE/Opera/Chrome
		},
		mouseout : function() {
			/*清除事件*/
			if (document.addEventListener) {
				document.removeEventListener('DOMMouseScroll', scrollFunc, false);
			}//W3C
			window.onmousewheel = document.onmousewheel = null;
			//IE/Opera/Chrome
		}
	});
	/*滚轮事件  */
	function scrollerMoving(str) {
		usescroller = true;
		st = parseInt(str) > 0 ? -1 : 1;
		n += (st * speed);
		usemouse ? smove = left + move : smove += (st * speed);
		move = 0;
		usemouse = false;
		if (smove < minmove) {
			smove = 0;
		} else if (smove > maxmove) {
			n -= (st * speed);
			smove = $("#" + config.scrollerArea).width() - swidth;
			if(!isend){
				isend = 1;
				onend(config.scrollerMoveBox);
			}
		}
		$("#" + config.scrollerMoveBox).css("margin-left", -smove * t);
		$("#" + config.scrollerArea).empty();
		$("#" + config.scrollerArea).append("<div class='scroller' style='margin-left:" + smove + "px; height:100%; width:" + swidth + "px; background-color:#9999ff;'></div>");
	}

	/*滚轮条初始化  */
	function init() {
		$("#" + config.scrollerArea).find(".scroller").live({
			mouseover : function() {
				$(this).css("background-color", "#5555ff");
			},
			mouseout : function() {
				$(this).css("background-color", "#9999ff");
			}
		})
	}

	var mw = $("#" + config.scrollerMoveBox).width();
	/*载体长度更新 */
	function widthRefresh(now,prew) {
		mw += (now-prew);
		isend = 0;
		swidth = ($("#" + config.scrollerContain).width() / mw * $("#" + config.scrollerArea).width());
		t = (mw - $("#" + config.scrollerArea).width()) / ($("#" + config.scrollerArea).width() - swidth);
		maxmove = $("#" + config.scrollerArea).width() - swidth;
		if (usemouse) {
			nleft = (prew / now) * left;
			left = nleft;
		} else {
			nleft = (prew / now) * smove;
			smove = nleft;
		}
		leftd = nleft;
		n = 0;
		$("#" + config.scrollerMoveBox).css("margin-left", -nleft * t);
		$("#" + config.scrollerArea).children("div").css("margin-left", nleft + 'px');
		$("#" + config.scrollerArea).children("div").width(swidth + 'px');
		dl = $('#'+config.scrollerArea).offset().left;
	}

	return widthRefresh;

}
